//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.baosight.xservices.ew.pd.service;

import com.baosight.bpm.bpmn.converter.BpmnConverter;
import com.baosight.bpm.domain.BpmCategory;
import com.baosight.bpm.domain.BpmProcessDef;
import com.baosight.bpm.domain.BpmProcessDefNode;
import com.baosight.bpm.domain.BpmProcessInstance;
import com.baosight.bpm.engine.BpmException;
import com.baosight.bpm.engine.WorkflowManager;
import com.baosight.bpm.manager.BpmCategoryManager;
import com.baosight.bpm.manager.BpmProcessDefManager;
import com.baosight.bpm.manager.BpmProcessInstanceManager;
import com.baosight.bpm.mxGraph.model.MxGraphModel;
import com.baosight.bpm.mxGraph.model.Root;
import com.baosight.bpm.mxGraph.model.WorkflowProcess;
import com.baosight.bpm.util.BPMEyeLog;
import com.baosight.bpm.util.DateFormatUtil;
import com.baosight.iplat4j.core.cache.CacheManager;
import com.baosight.iplat4j.core.data.ibatis.dao.Dao;
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.service.impl.ServiceEPBase;
import com.baosight.iplat4j.core.util.DateUtils;
import com.baosight.iplat4j.core.util.EiInfoUtil;
import com.baosight.iplat4j.core.util.StringUtils;
import com.baosight.iplat4j.core.web.threadlocal.UserSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.activiti.engine.impl.pvm.process.TransitionImpl;
import org.springframework.beans.factory.annotation.Autowired;

public class ServiceEWPD01 extends ServiceEPBase {
    @Autowired
    private BpmCategoryManager bpmCategoryManager;
    @Autowired
    private BpmProcessDefManager bpmProcessDefManager;
    @Autowired
    private BpmProcessInstanceManager bpmProcessInstanceManager;
    @Autowired
    private BpmnConverter bpmnConverter;
    @Autowired
    private Dao dao;

    public ServiceEWPD01() {
    }

    public EiInfo initLoad(EiInfo inInfo) {
        inInfo.addBlock("result").addBlockMeta(this.initResult());
        return inInfo;
    }

    public EiInfo query(EiInfo inInfo) {
        return super.query(inInfo);
    }

    public EiInfo getProcessDefDetail(EiInfo inInfo) throws ParseException {
        this.log("调用EWPD01中getProcessDefDetail方法开始");
        EiInfo outInfo = new EiInfo();
        String processDefId = inInfo.get("processDefId").toString();
        BpmProcessDef bpmProcessDef = this.bpmProcessDefManager.getProcessDef(processDefId);
        outInfo.set("xml", bpmProcessDef.getMxgraphXml());
        outInfo.set("ActProcDefId", bpmProcessDef.getActProcDefId());
        outInfo.set("ProcessDefinitionId", bpmProcessDef.getProcessDefId());
        outInfo.set("ProcessDefinitionKey", bpmProcessDef.getProcessKey());
        outInfo.set("ProcessDefinitionName", bpmProcessDef.getProcessName());
        outInfo.set("Description", bpmProcessDef.getDescription());
        BpmCategory bpmcategory = this.bpmCategoryManager.getCategory(bpmProcessDef.getCategory());
        outInfo.set("Category", bpmcategory.getCategoryName());
        String deploytime = "";
        if (bpmProcessDef.getDeploytime().trim().length() > 0) {
            Date time = DateFormatUtil.parseDateTime14(bpmProcessDef.getDeploytime());
            deploytime = DateFormatUtil.format(time);
        }

        outInfo.set("DeployTime", deploytime);
        outInfo.set("Author", bpmProcessDef.getAuthor());
        outInfo.set("ProcessVersion", bpmProcessDef.getProcessVersion());
        String state = bpmProcessDef.getState();
        if (state.equals("published")) {
            state = "已发布";
        }

        if (state.equals("draft")) {
            state = "未发布";
        }

        outInfo.set("ProcessStatus", state);
        this.log("调用EWPD01中getProcessDefDetail方法结束");
        return outInfo;
    }

    public EiInfo queryProcess(EiInfo inInfo) {
        this.log("调用EWPD01中queryProcess方法开始");
        String categoryId = (String)inInfo.get("inqu_status-0-categoryId");
        categoryId = StringUtils.defaultIfEmpty(categoryId, "");
        String name = (String)inInfo.get("inqu_status-0-name");
        String displayName = (String)inInfo.get("inqu_status-0-displayName");
        String status = (String)inInfo.get("inqu_status-0-status");
        String version = (String)inInfo.get("inqu_status-0-version");
        String desc = (String)inInfo.get("inqu_status-0-desc");
        String begTime = (String)inInfo.get("inqu_status-0-begTime");
        String endTime = (String)inInfo.get("inqu_status-0-endTime");
        EiBlock eiBlock = inInfo.getBlock("result");

        try {
            int offset = 0;
            int limit = 10;
            EiBlock inEiBlock = inInfo.getBlock("result");
            if (inEiBlock != null) {
                Map map = inEiBlock.getAttr();
                offset = Integer.parseInt(map.get("offset").toString());
                limit = Integer.parseInt(map.get("limit").toString());
            }

            Map<String, Object> parameter = new HashMap();
            parameter.put("category", categoryId);
            parameter.put("processKey", name);
            parameter.put("processName", displayName);
            parameter.put("state", status);
            parameter.put("version", version);
            parameter.put("description", desc);
            parameter.put("begTime", begTime);
            parameter.put("endTime", endTime);
            List processList = this.dao.query("EWPD01_A.queryMax", parameter, offset, limit);
            int count = Integer.parseInt(this.dao.query("EWPD01_A.countMax", parameter).get(0).toString());
            List list = new ArrayList();

            for(int i = 0; i < processList.size(); ++i) {
                BpmProcessDef bpmProcessDef = (BpmProcessDef)processList.get(i);
                Map map = new HashMap();
                map.put("ProcessDefinitionId", bpmProcessDef.getProcessDefId());
                map.put("ProcessDefinitionName", bpmProcessDef.getProcessKey());
                map.put("ProcessDefinitionDisplayName", bpmProcessDef.getProcessName());
                map.put("ProcessDefinitionDescription", bpmProcessDef.getDescription());
                map.put("ProcessVersion", bpmProcessDef.getProcessVersion());
                map.put("ProcessStatus", bpmProcessDef.getState());
                map.put("xml", bpmProcessDef.getMxgraphXml());
                list.add(map);
            }

            inEiBlock.setRows(list);
            inEiBlock.set("count", count);
            inInfo.setBlock(inEiBlock);
        } catch (Exception var21) {
            Exception e = var21;
            e.printStackTrace();
            inInfo.set("ProcessReturnValue", "ProcessReturnException");
        }

        inInfo.set("inqu_status-0-categoryId", categoryId);
        this.log("调用EWPD01中queryProcess方法结束");
        return inInfo;
    }

    public EiInfo saveProcessDefInfo(EiInfo inInfo) {
        this.log("调用EWPD01中saveProcessDefInfo方法开始");
        String operatemode = inInfo.get("processInfo-0-operateMode").toString();
        String category = inInfo.get("processInfo-0-parentId").toString();
        BpmProcessDef bpmProcessDef = (BpmProcessDef)EiInfoUtil.getBean(inInfo, "processInfo", 0, BpmProcessDef.class);
        if (operatemode != null && operatemode.equals("insert") && this.bpmProcessDefManager.countProcessDef(bpmProcessDef.getProcessKey()) > 0) {
            throw new BpmException("存在相同流程编码的流程定义！");
        } else {
            String processDefId = bpmProcessDef.getProcessDefId() == null ? " " : bpmProcessDef.getProcessDefId();
            if (bpmProcessDef.getState() == " ") {
                bpmProcessDef.setState("draft");
                bpmProcessDef.setProcessVersion("0");
            }

            bpmProcessDef.setCategory(category);
            bpmProcessDef.setAuthor(UserSession.getLoginName());
            bpmProcessDef.setDeploytime(DateUtils.curDateTimeStr14());
            this.bpmProcessDefManager.insertProcessDef(bpmProcessDef);
            inInfo.set("categoryId", category);
            inInfo.set("processDefId", processDefId);
            inInfo.set("processKey", bpmProcessDef.getProcessKey());
            inInfo.set("processDisplayName", bpmProcessDef.getProcessName());
            inInfo.set("description", bpmProcessDef.getDescription());
            this.log("调用EWPD01中saveProcessDefInfo方法结束");
            return inInfo;
        }
    }

    public EiInfo updateProcessDefInfo(EiInfo inInfo) {
        this.log("调用EWPD01中updateProcessDefInfo方法开始");
        BpmProcessDef bpmProcessDef = (BpmProcessDef)EiInfoUtil.getBean(inInfo, "processInfo", 0, BpmProcessDef.class);
        this.dao.update("EWPD01_A.updateProcess", bpmProcessDef);
        String description = bpmProcessDef.getDescription() == null ? "" : bpmProcessDef.getDescription();
        String processName = bpmProcessDef.getProcessName() == null ? "" : bpmProcessDef.getProcessName();
        bpmProcessDef = this.bpmProcessDefManager.getProcessDefByKey(bpmProcessDef.getProcessKey());
        bpmProcessDef.setDescription(description);
        bpmProcessDef.setProcessName(processName);
        Map<String, Object> cache = CacheManager.getCache("BPM:workflow");
        cache.put(bpmProcessDef.getProcessKey(), bpmProcessDef);
        cache.put(bpmProcessDef.getActProcDefId(), bpmProcessDef);
        cache.put(bpmProcessDef.getProcessDefId(), bpmProcessDef);
        this.log("调用EWPD01中updateProcessDefInfo方法结束");
        return inInfo;
    }

    public EiInfo saveCopyProcessDefInfo(EiInfo inInfo) {
        this.log("调用EWPD01中saveCopyProcessDefInfo方法开始");
        String operatemode = inInfo.get("processInfo-0-operateMode").toString();
        String category = inInfo.get("processInfo-0-parentId").toString();
        BpmProcessDef bpmProcessDef = (BpmProcessDef)EiInfoUtil.getBean(inInfo, "processInfo", 0, BpmProcessDef.class);
        if (operatemode != null && operatemode.equals("insert") && this.bpmProcessDefManager.countProcessDef(bpmProcessDef.getProcessKey()) > 0) {
            throw new BpmException("存在相同流程编码的流程定义！");
        } else {
            String originProcessDefId = inInfo.get("processInfo-0-originProcessDefId").toString();
            BpmProcessDef originProcessDef = this.bpmProcessDefManager.getProcessDef(originProcessDefId);
            String originMxgraphXml = originProcessDef.getMxgraphXml();
            MxGraphModel mxModel = this.bpmnConverter.translateMxGraphModel(originMxgraphXml);
            Root root = mxModel.getRoot();
            WorkflowProcess wp = root.getWorkflowProcess();
            wp.setSelfId(bpmProcessDef.getProcessDefId());
            wp.setName(bpmProcessDef.getProcessKey());
            wp.setDisplayName(bpmProcessDef.getProcessName());
            wp.setDescription(bpmProcessDef.getDescription());
            SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (中国标准时间)", Locale.ENGLISH);
            String currentDateTime = format.format(new Date());
            wp.setDeployTime(currentDateTime);
            wp.setAuthor(UserSession.getLoginName());
            wp.setVersion("0");
            wp.setStatus("draft");
            root.setWorkflowProcess(wp);
            mxModel.setRoot(root);
            String currentMxgraphXml = this.bpmnConverter.translateNodeToXml(mxModel);
            bpmProcessDef.setMxgraphXml(currentMxgraphXml);
            if (bpmProcessDef.getState() == " ") {
                bpmProcessDef.setState("draft");
                bpmProcessDef.setProcessVersion("0");
            }

            bpmProcessDef.setCategory(category);
            bpmProcessDef.setAuthor(UserSession.getLoginName());
            bpmProcessDef.setDeploytime(DateUtils.curDateTimeStr14());
            this.bpmProcessDefManager.insertProcessDef(bpmProcessDef);
            this.log("调用EWPD01中saveCopyProcessDefInfo方法结束");
            return inInfo;
        }
    }

    public EiInfo saveProcessImport(EiInfo inInfo) {
        this.log("调用EWPD01中saveProcessImport方法开始");
        String operatemode = inInfo.get("processInfo-0-operateMode").toString();
        String mxDefXml = inInfo.get("processInfo-0-workFlowXml").toString();
        String processDefId = inInfo.get("processInfo-0-processDefId").toString();
        String category = inInfo.get("processInfo-0-parentId").toString();
        MxGraphModel mxModel = this.bpmnConverter.translateMxGraphModel(mxDefXml);
        Root root = mxModel.getRoot();
        WorkflowProcess wp = root.getWorkflowProcess();
        BpmProcessDef bpmProcessDef = new BpmProcessDef();
        bpmProcessDef.setProcessDefId(processDefId);
        bpmProcessDef.setProcessKey(wp.getName());
        bpmProcessDef.setProcessName(wp.getDisplayName());
        bpmProcessDef.setDescription(wp.getDescription());
        bpmProcessDef.setCategory(category);
        bpmProcessDef.setDeploytime(DateUtils.curDateTimeStr14());
        bpmProcessDef.setAuthor(UserSession.getLoginName());
        bpmProcessDef.setProcessVersion("0");
        bpmProcessDef.setState("draft");
        wp.setAuthor(UserSession.getLoginName());
        wp.setVersion("0");
        wp.setStatus("draft");
        wp.setCategory(category);
        root.setWorkflowProcess(wp);
        mxModel.setRoot(root);
        String currentMxgraphXml = this.bpmnConverter.translateNodeToXml(mxModel);
        String replaceMxDefXml = currentMxgraphXml.replace("&#xa;", "");
        bpmProcessDef.setMxgraphXml(replaceMxDefXml);
        if (operatemode != null && operatemode.equals("insert") && this.bpmProcessDefManager.countProcessDef(bpmProcessDef.getProcessKey()) > 0) {
            throw new BpmException("存在相同流程编码的流程定义！");
        } else {
            this.bpmProcessDefManager.insertProcessDef(bpmProcessDef);
            this.log("调用EWPD01中saveProcessImport方法结束");
            return inInfo;
        }
    }

    public EiInfo delete(EiInfo inInfo) {
        this.log("调用EWPD01中delete方法开始");

        for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); ++i) {
            String processDefId = inInfo.getBlock(EiConstant.resultBlock).getRow(i).get("ProcessDefinitionId").toString();
            String state = inInfo.getBlock(EiConstant.resultBlock).getRow(i).get("state").toString();
            String processKey = inInfo.getBlock(EiConstant.resultBlock).getRow(i).get("processKey").toString();
            List<BpmProcessInstance> bpmProcessInstanceList = this.bpmProcessInstanceManager.getProcessInstancesByProcessDefId(processDefId);
            if (bpmProcessInstanceList.size() != 0) {
                throw new BpmException("无法删除有流程实例的流程定义！");
            }

            if ("draft".equals(state)) {
                HashMap paramMap = new HashMap();
                paramMap.put("processKey", processKey);
                this.dao.delete("EWPD01_A.deleteAuthorization", paramMap);
            }

            this.bpmProcessDefManager.deleteProcessDef(processDefId);
        }

        Map<String, Object> cache = CacheManager.getCache("BPM:workflow");
        if (cache != null) {
            cache.clear();
        }

        this.log("调用EWPD01中delete方法结束");
        return this.queryProcess(inInfo);
    }

    public EiInfo getProcessByProcessDefId(EiInfo inInfo) {
        this.log("调用EWPD01中getProcessByProcessDefId方法开始");
        String processKey = (String)inInfo.get("processKey");
        Map map = new HashMap();
        map.put("processKey", processKey);
        BpmProcessDef bpd = (BpmProcessDef)this.dao.query("EWPD01_A.queryMax", map).get(0);
        String form = bpd.getForm();
        if (form == null || form.equals("")) {
            List<BpmProcessDefNode> list = this.bpmProcessDefManager.getManualActivityOfProcess(bpd.getProcessDefId());
            List<TransitionImpl> transitionList = WorkflowManager.getSubsequentTransitionsOfProcessStartActivity(bpd.getProcessKey());
            BpmProcessDefNode firstManualActivity = null;

            for(int i = 0; i < list.size(); ++i) {
                if (((TransitionImpl)transitionList.get(0)).getDestination().toString().contains(((BpmProcessDefNode)list.get(i)).getNodeKey())) {
                    firstManualActivity = (BpmProcessDefNode)list.get(i);
                }
            }

            form = firstManualActivity.getWorkAddress();
        }

        inInfo.set("formName", form);
        this.log("调用EWPD01中getProcessByProcessDefId方法结束");
        return inInfo;
    }

    public EiInfo getFormByProcessDefId(EiInfo inInfo) {
        this.log("调用EWPD01中getFormByProcessDefId方法开始");
        String processDefName = inInfo.get("processDefName").toString();
        Map parameter = new HashMap();
        parameter.put("processDefKey", processDefName);
        BpmProcessDef processList = (BpmProcessDef)this.dao.query("EWPD01_A.queryMax", parameter).get(0);
        BpmProcessDef bpd = this.bpmProcessDefManager.getProcessDef(processList.getProcessDefId());
        String form = bpd.getForm();
        if (form == null || form.equals("")) {
            List<BpmProcessDefNode> list = this.bpmProcessDefManager.getManualActivityOfProcess(processList.getProcessDefId());
            List<TransitionImpl> transitionList = WorkflowManager.getSubsequentTransitionsOfProcessStartActivity(bpd.getProcessKey());
            BpmProcessDefNode firstManualActivity = null;

            for(int i = 0; i < list.size(); ++i) {
                if (((TransitionImpl)transitionList.get(0)).getDestination().toString().contains(((BpmProcessDefNode)list.get(i)).getNodeKey())) {
                    firstManualActivity = (BpmProcessDefNode)list.get(i);
                }
            }

            form = firstManualActivity.getWorkAddress();
        }

        inInfo.set("formName", form);
        this.log("调用EWPD01中getFormByProcessDefId方法结束");
        return inInfo;
    }

    public EiInfo updateCategory(EiInfo inInfo) {
        this.log("调用EWPD01中updateCategory方法开始");
        String category = (String)inInfo.get("Category");
        Map<String, Object> parameter = new HashMap();
        Map<String, Object> param = new HashMap();

        for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); ++i) {
            String processDefinitionId = inInfo.getBlock(EiConstant.resultBlock).getRow(i).get("ProcessDefinitionId").toString();
            BpmProcessDef bpmProcessDef = this.bpmProcessDefManager.getProcessDef(processDefinitionId);
            if ("published".equals(bpmProcessDef.getState())) {
                String mxgraphXml = bpmProcessDef.getMxgraphXml();
                MxGraphModel mxgm = this.bpmnConverter.translateMxGraphModel(mxgraphXml);
                Root root = mxgm.getRoot();
                WorkflowProcess wfp = root.getWorkflowProcess();
                wfp.setCategory(category);
                mxgraphXml = this.bpmnConverter.translateNodeToXml(mxgm);
                parameter.put("mxgraphXml", mxgraphXml);
                parameter.put("state", "published");
                parameter.put("processDefName", bpmProcessDef.getProcessKey());
                parameter.put("category", category);
                this.dao.update("EWPD01_A.updateCategory", parameter);
            }

            param.put("processDefName", bpmProcessDef.getProcessKey());
            param.put("category", category);
            this.dao.update("EWPD01_A.updateCategory", param);
        }

        this.log("调用EWPD01中updateCategory方法结束");
        return this.queryProcess(inInfo);
    }

    public EiInfo refresh(EiInfo inInfo) {
        this.log("调用EWPD01中refresh方法开始");
        EiInfo outInfo = new EiInfo();
        String categoryId = (String)inInfo.get("id");

        try {
            int offset = 0;
            int limit = 10;
            EiBlock inEiBlock = new EiBlock("result");
            Map<String, Object> parameter = new HashMap();
            parameter.put("category", categoryId);
            List processList = this.dao.query("EWPD01_A.query", parameter, offset, limit);
            List list = new ArrayList();

            for(int i = 0; i < processList.size(); ++i) {
                BpmProcessDef bpmProcessDef = (BpmProcessDef)processList.get(i);
                Map map = new HashMap();
                map.put("ProcessDefinitionId", bpmProcessDef.getProcessDefId());
                map.put("ProcessDefinitionName", bpmProcessDef.getProcessKey());
                map.put("ProcessDefinitionDisplayName", bpmProcessDef.getProcessName());
                map.put("ProcessDefinitionDescription", bpmProcessDef.getDescription());
                map.put("ProcessVersion", bpmProcessDef.getProcessVersion());
                map.put("ProcessStatus", bpmProcessDef.getState());
                list.add(map);
            }

            inEiBlock.setRows(list);
            inInfo.addBlock(inEiBlock);
        } catch (Exception var13) {
            Exception e = var13;
            e.printStackTrace();
            outInfo.set("ProcessReturnValue", "ProcessReturnException");
        }

        this.log("调用EWPD01中refresh方法结束");
        return inInfo;
    }

    public EiBlockMeta initResult() {
        EiBlockMeta eiMetadata = new EiBlockMeta();
        EiColumn eiColumn = new EiColumn("ProcessDefinitionName");
        eiColumn.setDescName("流程编码");
        eiMetadata.addMeta(eiColumn);
        eiColumn = new EiColumn("ProcessDefinitionDisplayName");
        eiColumn.setDescName("流程名称");
        eiMetadata.addMeta(eiColumn);
        eiColumn = new EiColumn("ProcessStatus");
        eiColumn.setDescName("状态");
        eiMetadata.addMeta(eiColumn);
        eiColumn = new EiColumn("ProcessDefinitionDescription");
        eiColumn.setDescName("描述信息");
        eiMetadata.addMeta(eiColumn);
        eiColumn = new EiColumn("Manage");
        eiColumn.setDescName("编辑");
        eiMetadata.addMeta(eiColumn);
        return eiMetadata;
    }

    public EiInfo start(EiInfo inInfo) {
        Map<String, Object> map = (Map)inInfo.get("map");
        String processDefinitionKey = "";
        String starterId = "";
        String businessKey = "";
        String formKey = "";
        String subject = "";
        String comment = "";
        Map<String, Object> variables = new HashMap();

        try {
            if (map.get("processDefinitionKey") != null && map.get("processDefinitionKey") != "") {
                processDefinitionKey = (String)map.get("processDefinitionKey");
                if (map.get("starterId") != null && map.get("starterId") != "") {
                    starterId = (String)map.get("starterId");
                    if (map.get("businessKey") != null && map.get("businessKey") != "") {
                        businessKey = (String)map.get("businessKey");
                    }

                    if (map.get("formKey") != null && map.get("formKey") != "") {
                        formKey = (String)map.get("formKey");
                    }

                    if (map.get("subject") != null && map.get("subject") != "") {
                        subject = (String)map.get("subject");
                    }

                    if (map.get("comment") != null && map.get("comment") != "") {
                        comment = (String)map.get("comment");
                    }

                    if (map.get("variables") != null && map.get("variables") != "") {
                        variables = (Map)map.get("variables");
                    }

                    String str = WorkflowManager.startProcess(processDefinitionKey, starterId, businessKey, formKey, subject, "", comment, (List)null, (Map)variables);
                    inInfo.set("processInstanceId", str);
                    inInfo.setStatus(1);
                    return inInfo;
                } else {
                    throw new BpmException("流程启动人ID不能为空!");
                }
            } else {
                throw new BpmException("流程定义ID不能为空!");
            }
        } catch (Exception var11) {
            Exception e = var11;
            BPMEyeLog.BPMlog("4007", this.getClass().getName(), Thread.currentThread().getStackTrace()[1].getMethodName() + "报错信息:" + e.getMessage());
            inInfo.setStatus(-1);
            inInfo.setMsg(e.getMessage());
            return inInfo;
        }
    }
}
